const MixinSsj = {
    data() {
        return {
               fileNames:'',
               actions: [{
                id: 0,
                text: '录音传邮箱',
                // icon: '../image/send3.png'
            },
            {
                id: 3,
                text: '录音传内网',
                // icon: '../image/tb2.png'
            },

        ],
        showShare:false,
        showPopover: false,
        showFile: false,
        showFileNw: false, //同步至中转站的弹框
        action: 0,
        progressBar: false,
        filesPdfName: '',
        filesPdfSize: '',
        // 设置每个分片的大小（字节）
        CHUNK_SIZE: 5 * 1024 * 1024 + "", // MB
        // 分片上传成功索引
        succeed: [],
        // 分片上传失败索引
        // failed: [],
        currentRate: 0,
        progress: 0,
        fileName: '',
        progressBar: false,
        retryNum: 0,
        formatList: ['mp3', 'wav', 'pcm', 'amr', 'm4a', "MP3", "WAV", "PCM", "AMR", "M4A"],
        disabledInp: false,
        showPopdel: false,
        filePathList: [], //选择文件夹的列表
        filePathId:''
        }
    },
    computed:{
        fileSizeClass() {
            return 'file-size-small';
        }
    },
    filters: {
       
        fileNametext: (value) => {
            if (!value) return "";
            let data = value.split("");
            if (data.length < 12) {
                return value;
            }
            return value.slice(0, 12) + "...";
        },
    },
   
    mounted() {
        // this.getList()
    },
    methods: {
        getList() {
            const userId = sessionStorage.getItem("userId");
            const orgId = sessionStorage.getItem("unitId")
            iscp_port = sessionStorage.getItem("iscp_port") || "";
            let data = {
                "orgId": orgId,
                "userId": userId,
                "timestamp": new Date().getTime() + ""
            };
            data = JSON.stringify(data)
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            var URL
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/defaultPath/searchDefaultPathList",
                type: "post",
                async: false, //是否同步，false表示同步请求
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    console.log(xhr.getResponseHeader("sign"),8989+"sign")

                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                                 this.filePathList = [{
                                        id: '0',
                                        userId: '0',
                                        orgId: '0',
                                        pathName: '最近打开',
                                        pathValue: '',
                                        sortOrder: null,
                                        createTime: '2025-08-20 16:04:28'
                                    },
                                    ...res.resultValue
                                ];

                            }
                        } else {
                            mui.toast(res.resultValue, {
                                duration: '1000',
                                type: 'div'
                            })
                        }
                    } else {
                        mui.toast('完整性校验失败', {
                            duration: '1000',
                            type: 'div'
                        })
                    }
                },
                error: (e) => {
                    mui.toast('请求异常,请稍后再试', {
                        duration: '1000',
                        type: 'div'
                    })
                },
            });
        },
    onSelect(action) {
            this.action = action.id
            if (action.id == 3) {
                this.showFileNw = true
            } else {
                this.showFile = true;
            }
        },
        
        delUplode() {
            let _that_ = this
            if (_that_.disabledInp) {
                _that_.showFileNw = false
                _that_.showFile = false
                var btnArray = ["取消", "确定"];
                var i = 0
                mui.confirm(
                    "是否终止上传？",
                    "消息提醒",
                    btnArray,
                    (e) => {
                        if (i > 0) return
                        i++
                        if (e.index == 1) {
                            console.log(_that_.channelId, "_that_.channelId")
                            wx.invoke("ext_Net_FragmentUploadCancel", {
                                data: {
                                    requestTag: _that_.channelId
                                }
                            }, function (response) {
                                console.log("ext_Net_FragmentUploadCancel end", response);
                            })
                            _that_.cleanData();
                        } else {
                            if (_that_.action == 3) {
                                _that_.showFileNw = true
                            } else {
                                _that_.showFile = true
                            }
                        }
                    }
                );
            } else {
                _that_.showFileNw = false
                _that_.showFile = false;
            }
        },
        checkFile() {
            // 开关是开启
            // 列表数据大于两条
            // 非苹果手机
          if(localStorage.getItem("showBtn") == 'true' && this.filePathList.length >= 2 && !this.isflageIOX ){
            console.log(999)
            this.showFileNw = false
            this.showFile = false
            this.showShare = true
          } else {
            this.checkFilefun()
          }
        },
        // 点击文件夹中的选项
        fileListBtn(item) {
            this.showShare = false
            if (this.action == 3) {
                this.showFileNw = true
                // this.checkFilefun(sessionStorage.getItem("liyuhang"))
            } else {
                this.showFile = true
                // this.checkFilefun(sessionStorage.getItem("liyuhang2"))
            }
            // alert(item.pathValue,"路径")

            this.checkFilefun(item)
        },
        // localStorage.setItem("showSuspension", val)
        checkFilefun(item) {
            // console.log(path,"路径")
            let _that = this
            if (_that.disabledInp) {
                mui.toast('正在上传文件...', {
                    duration: 'long',
                    type: 'div'
                })
                return
            }
            var funName = item&&(item.id != 0) ? "ext_FileBrowser_AppointDirChooseFile" : "ext_FileBrowser_StartPreView"
            var dataNAme = item&&(item.id != 0) ? { "appointDirPath": item.pathName} : {} 
            wx.invoke(funName, {
                data:dataNAme
            }, function (res) {
                _that.channelId = "net_upload_" + _that.createNonceStr(16) + new Date()
                    .getTime() + "";
                _that.progress = 0
                wx.invoke("multiWindows_subscribe", {
                    channelId: _that.channelId
                }, function (res) {
                    if (res.err_msg == "multiWindows_subscribe:ok") {
                        var progress = Number(res.data.progress) >= 100 ? 99 : Number(
                            res.data.progress)
                        _that.progress = progress > _that.progress ? progress : _that
                            .progress
                    }
                })
                if (_that.isflageIOX) {
                    const suffix = res.name.split('.').reverse()[0]
                    if (!_that.formatList.includes(suffix)) {
                        mui.toast('选择的文件格式不支持', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    if (checkFileNameForSensitiveWords(res.name)) {
                        mui.toast('检测到敏感内容，请确认后再试。', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    if (Number(res.size) / 400000000 > 1) {
                        mui.toast('上传文件大小不能大于400MB', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    if (Number(res.size) / 100000000 > 1) {
                        _that.CHUNK_SIZE = 10 * 1024 * 1024 + ""
                    }
                    if (Number(res.size) / 40000000 > 1  && _that
                        .action == 0) {
                        mui.toast('上传文件大小不能大于40MB', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    _that.disabledInp = true
                    _that.checkList(res.filePath, res.name, _that.channelId)
                } else {
                    var fileName
                    var fileSize
                    var filePath
                    if(res.name) {
                        fileName = res.name
                        fileSize = res.size
                        filePath = res.filePath
                    } else {
                        let recordArr = JSON.parse(res.result)
                        fileName = recordArr[0].name
                        fileSize = recordArr[0].fileSize
                        filePath = recordArr[0].path
                    }
                    const suffix = fileName.split('.').reverse()[0]
                    if (!_that.formatList.includes(suffix)) {
                        mui.toast('选择的文件格式不受支持', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    if (checkFileNameForSensitiveWords(fileName)) {
                        mui.toast('检测到敏感内容，请确认后再试。', {
                            duration: 'long',
                            type: 'div'
                        })
                        return false
                    }
                    if (Number(fileSize) / 100000000 > 1) {
                        _that.CHUNK_SIZE = 10 * 1024 * 1024 + ""
                    }
                    if (Number(fileSize) / 400000000 > 1) {
                        mui.toast('上传文件大小不能大于400MB', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    if (Number(fileSize) / 40000000 > 1 && _that.action == 0) {
                        mui.toast('上传文件大小不能大于40MB', {
                            duration: 'long',
                            type: 'div'
                        })
                        return
                    }
                    _that.disabledInp = true
                    _that.checkList(filePath, fileName, _that.channelId)
                }
            })
        },

        checkList(path, name, id, hash, index) {
            let that = this
            that.progressBar = true
            let iscp_port
            iscp_port = sessionStorage.getItem("iscp_port") || "";
            let Authentication = sessionStorage.getItem("Authentication");
            const timestamp = new Date().getTime() + ""
            let userId = sessionStorage.getItem("userId");
            let sign = sm3(timestamp + "&" + userId)
            that.fileName = name
            let data = {
                serverAddress: localUrlLink + iscp_port +
                    `/tjAiAssistant/storage/splitUpload2?channelId=${id}`,
                filePath: path,
                fragmentSize: that.CHUNK_SIZE,
                fileName: name,
                fileHash: hash ? hash : '',
                upLoadFragmentIndex: index ? index : '',
                channelId: id,
                headerData: {
                    sign: sign,
                    userId: userId,
                    type: '0',
                    timestamp: timestamp,
                    Authentication: Authentication,
                }
            }
            wx.invoke("ext_Net_FragmentUpload", {
                data: data,
            }, function (res) {
                if (res.err_msg.endsWith(":ok")) { //成功结果
                    //上传完成
                        that.sendEmail(id,name)

                } else if (res.err_msg.endsWith(":cancel")) {
                    that.cleanData();
                    return
                } else {
                    //上传失败
                    if (that.myindex != Number(res.upLoadFragmentIndex)) {
                        that.myindex = Number(res.upLoadFragmentIndex);
                        that.retryNum = 0;
                    }
                    setTimeout(() => {
                        if (that.retryNum > 4) {
                            // that.retryNum = 0
                            if (that.action == 3) {
                                that.showFileNw = false
                            } else {
                                that.showFile = false
                            }
                            var btnArray = ["取消", "确定"];
                            var i = 0
                            mui.confirm(
                                "是否进行重试？",
                                "上传失败",
                                btnArray,
                                (e) => {
                                    if (i > 0) return
                                    i++
                                    if (e.index == 1) {
                                        if (that.action == 3) {
                                            that.showFileNw = true
                                        } else {
                                            that.showFile = true
                                        }
                                        that.checkList(path, name, id, res.fileHash,
                                            res.upLoadFragmentIndex)
                                    } else {
                                        that.cleanData()
                                    }
                                }
                            );
                        } else {
                            that.retryNum++
                            that.checkList(path, name, id, res.fileHash, res
                                .upLoadFragmentIndex)
                        }
                    }, (that.retryNum + 1) * 1000)
                }
            })
        },
        // 右上角发送邮箱
        sendEmail(id,name) {
            userId = sessionStorage.getItem("userId");
            unitId = sessionStorage.getItem("unitId");
            iscp_port = sessionStorage.getItem("iscp_port") || "";
            let data = {
                "id": id,
                "sceneNo":64,
                "messageTitle": name,
                "userId": userId,
                "saveType": this.action,
                "unitId": unitId,
                "timestamp": new Date().getTime() + ""
            };
            data = JSON.stringify(data)
            let sign = sm3(data);
            let AuthenticationSession = sessionStorage.getItem("Authentication");
            $.ajax({
                url: localUrlLink + iscp_port + "/tjAiAssistant/Recording/recordSendMail",
                type: "post",
                async: false,
                dataType: "json",
                data: data,
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    Authentication: AuthenticationSession,
                    sign: sign
                },
                success: (res, txt, xhr) => {
                    if (sm3(res.resultValueStr) == xhr.getResponseHeader("sign")) {
                        let Authentication = xhr.getResponseHeader("Authentication")
                        if (res.code == 200) {
                            if (Authentication) {
                                sessionStorage.setItem("Authentication", Authentication);
                                this.progress = 100
                                if (this.action == 3) {
                                    showConfirmDialog("同步完成，请及时到内网网站aioa.sgcc.com.cn下载使用"," ")
                                } else {
                                    mui.toast('上传成功,已发送到内网邮箱', {
                                        duration: 1000,
                                        type: 'div'
                                    })
                                }

                                setTimeout(() => {
                                    this.cleanData()
                                }, 1500)
                            }
                        } else if (res.code == 500) {
                            if (this.action == 3) {
                                syncToast('同步失败', 1500, "error", true)
                            } else {
                                mui.toast(res.resultValue, {
                                    duration: 'long',
                                    type: 'div'
                                })
                            }

                        } else {
                            mui.toast('请求异常,请稍后再试', {
                                duration: 'long',
                                type: 'div'
                            })
                        }
                    } else {
                        mui.toast('完整性校验失败', {
                            duration: 'long',
                            type: 'div'
                        })
                    }
                },
                error: (e) => {
                    // $(".curtainVoice").css("display", "none");
                    mui.toast('请求异常,请稍后再试', {
                        duration: 'long',
                        type: 'div'
                    })
                },
            });
        },
        cleanData() {
            this.showPopdel = false
            this.showFile = false;
            this.showFileNw = false;
            this.disabledInp = false
            this.progressBar = false
            
        },
       
    },
};